package cn.edu.sctu.sctu_lysd.view.act;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.biz.DepositBiz;
import cn.edu.sctu.sctu_lysd.cofig.CacheKeyConfig;
import cn.edu.sctu.sctu_lysd.cofig.Status;
import cn.edu.sctu.sctu_lysd.databinding.ActivityDepositBinding;
import cn.edu.sctu.sctu_lysd.presenter.DepositPresenter;
import cn.edu.sctu.sctu_lysd.utils.SharePreferenceUtils;

/**
 * Created by wangfuxin on 2017-04-19.
 */

public class DepositAct extends BaseActivity implements DepositBiz.View{

    private ActivityDepositBinding mBinding;

    private DepositBiz.Presenter mPresenter;

    private SharePreferenceUtils sharePreferenceUtils;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_deposit;
    }

    @Override
    protected void initAfter(Object bind) {

        mBinding = (ActivityDepositBinding) bind;

        initPresenter(new DepositPresenter(this,this));

        sharePreferenceUtils = SharePreferenceUtils.getInstance(this);

        mBinding.back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initBtn();


        mPresenter.getDeposit();
        mPresenter.getDepositOrder();

    }

    private void initBtn(){

        if (sharePreferenceUtils.ifHaveShare(CacheKeyConfig.CACHE_IS_DEPOSIT)) {

            if (sharePreferenceUtils.getInt(CacheKeyConfig.CACHE_IS_DEPOSIT) == Status.DEPOSIT_NO){

                mBinding.submitButton.setClickable(true);

                mBinding.submitButton.setText("确认支付");

            }

            if (sharePreferenceUtils.getInt(CacheKeyConfig.CACHE_IS_DEPOSIT) == Status.DEPOSIT_YES){

                mBinding.submitButton.setText("您已交付定金");

                mBinding.submitButton.setBackgroundColor(Color.parseColor("#FF434242"));

                mBinding.submitButton.setClickable(false);

            }


        }

    }

    @Override
    public void initPresenter(DepositBiz.Presenter presenter) {

        mPresenter = presenter;

        mPresenter.start();

    }

    @Override
    public void getDepositOrderSuccess(final long orderID) {

        mBinding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.intentPay(orderID);
            }
        });


    }

    @Override
    public void getDepositOrderFailed() {

    }

    /**
     * 用户已缴纳定金
     */
    @Override
    public void userAlreadyDeposit() {

        Toast.makeText(this,"您已缴纳押金",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void getDepositSuccess(float amount) {

        TextView textView = (TextView) findViewById(R.id.price);

        textView.setText(amount + "元");


    }

    @Override
    public void getDepositFailed() {
        Toast.makeText(this,"网络错误",Toast.LENGTH_SHORT).show();
    }
}
